package cn.tensquare.spit.web.controller;

import cn.tensquare.spit.po.Spit;
import cn.tensquare.spit.service.SpitService;
import constants.StatusCode;
import dto.PageResultDTO;
import dto.ResultDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.concurrent.TimeUnit;

/**
 *
 */
@RestController
@CrossOrigin
@RequestMapping("/spit")
public class SpitController {

    @Autowired
    private SpitService spitService;

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    @PutMapping("/thumbup/{id}")
    public ResultDTO incrementThumbup(@PathVariable String id){

        String userid = "2013";
        String redisKey = "thumbup_"+userid+"_"+id;
        if(null!=redisTemplate.opsForValue().get(redisKey)){
            return new ResultDTO(false,StatusCode.REPERROR,"你已经点过赞了");
        }
        //24小时内没有点过赞的
        //点赞
        spitService.updateSpitThumbupToIncrementing(id);
        //在redis中记录一下：将该用户和吐槽信息放入Redis缓存，并设置24小时内有效
        redisTemplate.opsForValue().set(redisKey,"1",15, TimeUnit.SECONDS);

        /*spitService.updateSpitThumbupToIncrementing(id);*/
        return new ResultDTO(true,StatusCode.OK,"点赞成功");
    }


    @GetMapping("/comment/{parentid}/{page}/{size}")
    public ResultDTO listPageByParentid(@PathVariable String parentid,@PathVariable int page,@PathVariable int size){
        Page<Spit> byParentid = spitService.findByParentid(parentid, page, size);
        return new ResultDTO(true,StatusCode.OK,"查询成功",new PageResultDTO<Spit>(byParentid.getTotalElements(),byParentid.getContent()));
    }

    /**
     * 增加
     * @param spit
     */
    @PostMapping
    public ResultDTO add(@RequestBody Spit spit){
        spitService.saveSpit(spit);
        return new ResultDTO(true, StatusCode.OK,"增加成功");
    }

    /**
     * 修改
     * @param spit
     */
    @PutMapping("/{id}")
    public ResultDTO edit(@RequestBody Spit spit, @PathVariable String id ){
        spit.setId(id);
        spitService.updateSpit(spit);
        return new ResultDTO(true,StatusCode.OK,"修改成功");
    }

    /**
     * 删除
     * @param id
     */
    @DeleteMapping("/{id}")
    public ResultDTO remove(@PathVariable String id ){
        spitService.deleteSpitById(id);
        return new ResultDTO(true,StatusCode.OK,"删除成功");
    }

    /**
     * 查询全部数据
     * @return
     */
    @GetMapping
    public ResultDTO list(){
        return new ResultDTO(true,StatusCode.OK,"查询成功",spitService.findSpitList());
    }

    /**
     * 根据ID查询
     * @param id ID
     * @return
     */
    @GetMapping("/{id}")
    public ResultDTO listById(@PathVariable String id){
        return new ResultDTO(true,StatusCode.OK,"查询成功",spitService.findSpitById(id));
    }
}
